package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/user")
@RestController
public class UserController {


    @Reference
    private UserService userService;
    @RequestMapping("/getUsername")
    public Result getUsername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user!=null){
            String username = user.getUsername();
            String pic = userService.findUserPortraitByUsername(username);
            Map<String,String> map = new HashMap<>();
            map.put("username",username);
            map.put("imagesUrl",pic);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,map);
        }

        return new Result(false,MessageConstant.GET_USERNAME_FAIL);
    }

    /*findMenu*/

    @RequestMapping("/findMenu")
    public Result findMenu(){
        List<Map<String,Object>> list = userService.findMenu();
        List<Map<String,Object>> inList = new ArrayList<>();

        Map<String,Object> map = null;
        if(list!=null && list.size()>0){
            for (Map<String, Object> objectMap : list) {
                if(objectMap.get("parentMenuId") == null){
                    Integer id = (Integer) objectMap.get("id");
                    String  label = (String) objectMap.get("label");
                    map = new HashMap<>();
                    map.put("id",id);
                    map.put("label",label);

                    List<Map<String, Object>> menuById = userService.findMenuById(id);

                    List<Map<String, Object>> list1 = new ArrayList<>();
                    if(menuById!=null){
                        for (Map<String, Object> stringObjectMap : menuById) {
                            list1.add(stringObjectMap);
                        }
                        map.put("children",list1);

                    }
                    inList.add(map);
                }
            }

        }


        return new Result(true,MessageConstant.GET_USERNAME_FAIL,inList);
    }


    @RequestMapping("/getItems")
    public Result getItems(){
        List<Map<String,Object>> list = userService.findMenu();
        List<Integer> items = new ArrayList<>();
        if(list!=null && list.size()>0) {
            for (Map<String, Object> objectMap : list) {
                if (objectMap.get("parentMenuId") == null) {
                    Integer id = (Integer) objectMap.get("id");
                    items.add(id);
                }
            }
        }

        return new Result(true,"success",items);
    }


}
